package com.lam.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.base.domain.tree.TreeNode;
import com.lam.common.base.domain.tree.TreeUtil;
import com.lam.common.base.page.TableHelper;
import com.lam.common.utils.JsonUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.system.entity.SysFrontMenu;
import com.lam.system.param.SysFrontMenuParam;
import com.lam.system.service.SysFrontMenuService;
import com.lam.system.util.MenuUtil;

/**
 * 前端菜单Controller
 * 
 * @author cl
 * @date 2022-08-25
 */
@Log(title = "前端菜单")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/sys/sysFrontMenu")
public class SysFrontMenuController extends BaseController {

    @Autowired
    private SysFrontMenuService sysFrontMenuService;
    
    /**
	 * @return
	 */
    @Log(oper = "前端菜单列表")
	@RequestMapping(value = "/frontMenuList", method = RequestMethod.GET)
	public Result frontMenuList() {
    	
		//全部权限ids
		SysFrontMenuParam param = new SysFrontMenuParam();
		TableHelper.orderByColumn("menu_code", "asc");
		List<SysFrontMenu> list = sysFrontMenuService.selectList(param);
		
		List<String> allIdList = list.stream().map(t->t.getId()).collect(Collectors.toList());
		List<TreeNode> treeNodeList = JsonUtil.toJavaList(list, TreeNode.class, "id", "parentId", "menuName=>title", "menu_icon=>icon", "sortNo");
		Map<String, Object> json = new HashMap<>();
        json.put("treeData", TreeUtil.buildTree(treeNodeList, MenuUtil.MENU_TOP_ID));
        json.put("allIdList", allIdList);
        return Result.ok(json);
	}
    
    /**
     * 查询前端菜单列表
     */
    @Log(oper="分页查询")
    @PreAuthorize("@ss.hasPm('sys:sysFrontMenu:list')")
    @GetMapping("/list")
    public Result list(SysFrontMenuParam param) {
        //startPage();
        TableHelper.orderByColumn("menu_code", "asc");
        List<SysFrontMenu> list = sysFrontMenuService.selectList(param);
        TreeUtil.buildTree(list, MenuUtil.MENU_TOP_ID);
        return Result.ok(getTableData(list));
    }
    
    /**
     * 获取前端菜单详细信息
     */
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('sys:sysFrontMenu:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(sysFrontMenuService.selectById(id));
    }
    
    /**
     * 新增前端菜单
     */
    @PreAuthorize("@ss.hasPm('sys:sysFrontMenu:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody SysFrontMenu sysFrontMenu) {
    
        return toAjax(sysFrontMenuService.insert(sysFrontMenu));
    }
    
    /**
     * 修改前端菜单
     */
    @PreAuthorize("@ss.hasPm('sys:sysFrontMenu:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody SysFrontMenu sysFrontMenu) {
    
        return toAjax(sysFrontMenuService.update(sysFrontMenu));
    }
    
    /**
     * 删除前端菜单
     */
    @PreAuthorize("@ss.hasPm('sys:sysFrontMenu:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        return toAjax(sysFrontMenuService.deleteById(id));
    }

}
